package com.xiaoxi.bms.controller.quick;


import com.baomidou.mybatisplus.core.metadata.IPage;

import com.xiaoxi.bms.common.constant.constant.OperationConstants;
import com.xiaoxi.bms.common.constant.enumeration.dict.CodeModelDictEnums;
import com.xiaoxi.bms.common.constant.enumeration.statuscode.system.CodeModelSystemEnums;
import com.xiaoxi.bms.common.constant.enumeration.statuscode.user.CodeModelUserEnums;
import com.xiaoxi.bms.common.exception.BMSException;
import com.xiaoxi.bms.common.response.ApiResponse;
import com.xiaoxi.bms.common.util.ExcelUtils;
import com.xiaoxi.bms.domain.quick.BmsCodeModel;
import com.xiaoxi.bms.domain.quick.dto.BMSCodeModelDTO;
import com.xiaoxi.bms.domain.quick.vo.BMSCodeCorrDictVO;
import com.xiaoxi.bms.domain.quick.vo.BMSCodeModelVO;
import com.xiaoxi.bms.service.common.aspect.operation.OperationLog;
import com.xiaoxi.bms.service.common.circulation.ExportDataToExcelService;
import com.xiaoxi.bms.service.quick.BmsCodeModelService;
import com.xiaoxi.bms.service.quick.core.ICode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName BmsCodeModelController
 * @Description
 * @Author xiaoxi
 * @Date 2022-11-05
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/quick/api/v1/codeModel")
public class BmsCodeModelController {

    @Resource
    private BmsCodeModelService bmsCodeModelService;

    @Resource(name = "bmsCodeModelServiceImpl")
    private ExportDataToExcelService exportDataToExcelService;

    @Resource
    private Map<String, ICode<?>> codeMap;


    /**
     * 新增
     *
     * @param bmsCodeModelVO
     * @return
     */
    @OperationLog(operation = OperationConstants.CODE_CREATE)
    @PostMapping("/add")
    public ApiResponse add(@RequestBody BMSCodeModelVO bmsCodeModelVO) {
        try {
            bmsCodeModelService.add(bmsCodeModelVO);
            return ApiResponse.ofSuccess(bmsCodeModelVO);
        } catch (BMSException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.ofException(e);
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @OperationLog(operation = OperationConstants.CODE_DELETE)
    @GetMapping("/delete/{id}")
    public ApiResponse delete(@PathVariable("id") Long id) {
        try {
            bmsCodeModelService.delete(id);
            return ApiResponse.ofSuccess(id);
        } catch (BMSException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.ofException(e);
        }
    }

    /**
     * 更新
     *
     * @param bmsCodeModelVO
     * @return
     */
    @OperationLog(operation = OperationConstants.CODE_MODIFY)
    @PostMapping("/update")
    public ApiResponse update(@RequestBody BMSCodeModelVO bmsCodeModelVO) {
        try {
            bmsCodeModelService.update(bmsCodeModelVO);
            return ApiResponse.ofSuccess(bmsCodeModelVO);
        } catch (BMSException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.ofException(e);
        }
    }

    /**
     * 查询分页数据
     *
     * @param bmsCodeModelDTO
     * @param pageable
     * @return
     */
    @OperationLog(operation = OperationConstants.CODE_QUERY_PAGE)
    @GetMapping("/findListByPage")
    public ApiResponse findListByPage(BMSCodeModelDTO bmsCodeModelDTO, Pageable pageable) {
        try {
            IPage<BMSCodeModelVO> result = bmsCodeModelService.findListByPage(bmsCodeModelDTO, pageable);
            return ApiResponse.ofSuccess(result);
        } catch (BMSException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.ofException(e);
        }
    }

    /**
     * ID查询
     *
     * @param id
     * @return
     */
    @OperationLog(operation = OperationConstants.CODE_QUERY_ONE)
    @GetMapping("/findById/{id}")
    public ApiResponse findById(@PathVariable Long id) {
        try {
            BmsCodeModel bmsCodeModel = bmsCodeModelService.findById(id);
            return ApiResponse.ofSuccess(bmsCodeModel);
        } catch (BMSException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.ofException(e);
        }
    }


    @OperationLog(operation = OperationConstants.CODE_CHECK)
    @GetMapping("checkCodeType")
    public ApiResponse checkCodeType(String codeType, Long id) {

        Boolean repeat = bmsCodeModelService.checkCodeType(codeType, id);
        if (repeat) {
            return ApiResponse.ofEnum(CodeModelUserEnums.CODE_TYPE_EXIST);
        }
        return ApiResponse.ofSuccess();

    }

    @OperationLog(operation = OperationConstants.CODE_CHECK)
    @GetMapping("checkCodeName")
    public ApiResponse checkCodeName(String codeName, Long id) {

        Boolean repeat = bmsCodeModelService.checkName(codeName, id);
        if (repeat) {
            return ApiResponse.ofEnum(CodeModelUserEnums.CODE_NAME_EXIST);
        }
        return ApiResponse.ofSuccess();

    }

    @OperationLog(operation = OperationConstants.CODE_ENABLE)
    @GetMapping("enableCodeModel/{id}")
    public ApiResponse enableCodeModel(@PathVariable Long id) {

        try {
            bmsCodeModelService.enableCodeModel(id);
        } catch (BMSException e) {
            log.error("更新启用状态失败：{}", e.getMessage());
            return ApiResponse.ofException(e);
        }
        return ApiResponse.ofSuccess();

    }

    @GetMapping("exportCodeModelInfo")
    public void enableCodeModel(BMSCodeModelDTO bmsCodeModelDTO, HttpServletRequest request, HttpServletResponse response) {

        try {
            ExcelUtils.exportExcel(request, response, "代码模型表.xls", exportDataToExcelService, bmsCodeModelDTO);
        } catch (BMSException e) {
            throw new RuntimeException(e);
        }

    }

    @GetMapping("getCodeCorrDictList")
    public ApiResponse getCodeCorrDictList(String codeType) {

        BmsCodeModel bmsCodeModel = bmsCodeModelService.getCodeModel(codeType);
        if (Objects.isNull(bmsCodeModel)) {
            return ApiResponse.ofEnum(CodeModelSystemEnums.CODE_TYPE_NOT_EXIST);
        }

        String seqType = bmsCodeModel.getSeqType();

        List<?> codeList = null;
        try {
            codeList = codeMap.get(seqType).getCodeList(bmsCodeModel);
        } catch (BMSException e) {
            log.error("未知错误：{}", e.getMessage());
            return ApiResponse.ofException(e);
        }

        BMSCodeCorrDictVO bmsCodeCorrDictVO = new BMSCodeCorrDictVO();
        BeanUtils.copyProperties(bmsCodeModel, bmsCodeCorrDictVO);
        bmsCodeCorrDictVO.setCodeList(codeList);

        return ApiResponse.ofSuccess(bmsCodeCorrDictVO);
    }

    @PostMapping("compileRun")
    public ApiResponse compileRun(@RequestBody BmsCodeModel bmsCodeModel) {

        try {
            codeMap.get(CodeModelDictEnums.SEQ.getCode()).getCodeList(bmsCodeModel);
        } catch (BMSException e) {
            log.error("未知错误：{}", e.getMessage());
            return ApiResponse.ofException(e);
        }

        return ApiResponse.ofSuccess();

    }

}
